<template>
  <div class="container" v-loading="loading">
    <div class="frame">
      <el-form :model="ruleForm" status-icon
               :rules="rules" ref="ruleForm" label-width="100px"
               class="my-form"
      >
        <el-form-item label="管理员名" prop="manageName">
          <el-input type="text" v-model="ruleForm.manageName" autocomplete="off"
                    placeholder="请输入管理员名" width="600px"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label-width="0">
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "LoginView",
  data() {
    return {
      ruleForm: {
        manageName: '',
        password: '',
      },
      rules: {
        manageName: [
          {required: true, message: '管理员名不能为空', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '密码不能为空', trigger: 'blur'},
        ]
      },
      loading: false
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          axios.post('/manage/login', {
            manageName: this.ruleForm.manageName,
            password: this.ruleForm.password
          },{
            noIntercept: true // 不要拦截这个请求
          })
              .then(response => {
                this.loading = false;
                // 获取响应信息
                let data = response.data;
                // 登录失败
                if (data.code!== 200) {
                  this.$message.error(data.msg);
                  return;
                }
                // 登录成功
                this.$message({
                  message: data.msg,
                  type: 'success'
                });
                // 保存管理员信息到 localStorage
                localStorage.setItem("manageInfo", JSON.stringify(data.data));
                // 保存token
                localStorage.setItem("manageToken", data.token);
                // 跳转页面
                this.$router.push({
                  path: '/menu'
                })
              })
              .catch(error => {
                this.loading = false;
                console.log(error)
                this.$message.error('系统异常，请联系管理员或稍后再试');
              })
              .finally(() => this.loading = false);
        } else {
          this.loading = false;
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.container {
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
  height: 100vh; /* 设置容器高度为视口高度 */
}
.my-form {
  width: 350px;
}
.frame{
  padding: 50px 50px 28px;
  border-radius: 20px;
  box-shadow: rgba(0, 0, 0, 0.15) 1.95px 1.95px 2.6px;
}
</style>